package com.zxq.exchange.register;

import com.zxq.exchange.enums.EFrame;
import com.zxq.exchange.model.ExchangeDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 通知平台，有消息上报
 */
public class NotifyPlatformRegister {

    /**
     * 注册中心
     */
    static ConcurrentHashMap<EFrame,NotifyPlatformRegister> notifyPlatformRegisterHashMap = new ConcurrentHashMap<EFrame,NotifyPlatformRegister>();

    /**
     * 协议id
     * @param tcpFrame 协议id
     * @return
     */
    public static NotifyPlatformRegister getInstance(EFrame tcpFrame) {
        if(notifyPlatformRegisterHashMap.get(tcpFrame) == null) {
            NotifyPlatformRegister notifyPlatformRegister = new NotifyPlatformRegister();
            notifyPlatformRegisterHashMap.put(tcpFrame,notifyPlatformRegister);
        }
        return notifyPlatformRegisterHashMap.get(tcpFrame);
    }

    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,8,30L, TimeUnit.SECONDS,new ArrayBlockingQueue<>(10));
    /**
     * 消息注册监听
     */
    List<INotifyPlatform> registerCenter = new ArrayList<>();

    /**
     * 注册监听服务
     * @param listen
     */
    public void registerNotify(INotifyPlatform listen){
        registerCenter.add(listen);
    }

    /**
     * 消息提醒
     */
    public void notifyMessage(ExchangeDTO exchangeDTO) {
        threadPoolExecutor.execute(() ->{
            registerCenter.forEach(listen -> {
                listen.notifyPlatform(exchangeDTO);
            });
        });
    }





}
